package com.tsing.cli.system.controller;

import cn.hutool.core.lang.tree.Tree;
import com.tsing.cli.common.annotation.AnonymousPostMapping;
import com.tsing.cli.common.annotation.Limit;
import com.tsing.cli.common.annotation.Logger;
import com.tsing.cli.common.constant.LimitTypeEnum;
import com.tsing.cli.common.vo.ResultData;
import com.tsing.cli.system.service.IAuthorizationService;
import com.tsing.cli.system.vo.req.LoginReqVO;
import com.tsing.cli.system.vo.resp.LoginRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author TheTsing
 * @since 2022-11-25 15:46
 */
@Tag(name = "登录认证")
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthorizationController {

    private final IAuthorizationService authorizationService;

    @AnonymousPostMapping("/login")
    @Operation(summary = "登录")
    @Logger(description = "登录")
    @Limit(period = 500, count = 1, limitType = LimitTypeEnum.METHOD_AND_IP)
    public ResultData<LoginRespVO> login(@Validated @RequestBody LoginReqVO reqVO, HttpServletRequest request) {
        return ResultData.success(authorizationService.login(reqVO, request));
    }

    @GetMapping("/build")
    @Operation(summary = "获取权限列表")
    public ResultData<List<Tree<String>>> build() {
        return ResultData.success(authorizationService.build());
    }

    @DeleteMapping("/logout")
    @Operation(summary = "退出登录")
    public ResultData logout(HttpServletRequest request) {
        authorizationService.logout(request);
        return ResultData.success();
    }

}
